/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;

/**
 *
 * @author HuyNVK
 */
public class StringDate {
    /**
     * Create SQlDate from given date, month, year
     * @param date Date
     * @param month Month
     * @param year Year
     */
    public static java.sql.Date createDate(String date, String month, String year) throws Exception {
        if((date == null || date.isEmpty()) && (month == null || month.isEmpty()) && 
                (year == null || year.isEmpty())) {
            return null;
        } else if((date == null || date.isEmpty()) || (month == null || month.isEmpty()) || 
                (year == null || year.isEmpty())) {
            throw new Exception("Some of Input values is null.");
        } else {
//            int dateInt, monthInt, yearInt;
//            yearInt = Integer.parseInt(year);
//            monthInt = Integer.parseInt(month);
//            dateInt = Integer.parseInt(date);
//            java.util.Date tempDate = new java.util.Date(yearInt, monthInt, dateInt);
            java.util.Date tempDate = new java.util.Date(month + "/" + date + "/" + year);
            return new java.sql.Date(tempDate.getTime());
        }        
    }
    
    /**
     * Create SQLDate from HttpServletRequest and parameter names.
     * @param request HttpServletRequest.
     * @param date Name of parameter to get String Date from request
     * @param month Name of parameter to get String Month from request
     * @param year Name of parameter to get String Year from request
     */
    public static java.sql.Date createDate(HttpServletRequest request, String date, 
            String month, String year) throws Exception {
        controller.Controller.log.info("createDate");
        if(request == null) {
            throw new Exception("The request variable is null.");
        } else if((year == null) || (month == null) || (date == null)) {
            throw new Exception("Some of the given parameter strings is null");            
        } else {
            String strDate = request.getParameter(date);
            String strMonth = request.getParameter(month);
            String strYear = request.getParameter(year);
            controller.Controller.log.info("date: " + strDate);
            controller.Controller.log.info("month: " + strMonth);
            controller.Controller.log.info("year: " + strYear);
            
            return createDate(strDate, strMonth, strYear);
        }
    }
    
    public static void insertDateBoxValue(JspWriter out) throws IOException {
        out.print("<option></option>");
        for(int i = 1; i <=31; i++) {
            out.print("<option value='"+i+"'>"+i+"</option>");
        }
    }
    
    public static void insertDateBoxValue(JspWriter out, int selectedValue) throws IOException {
        out.print("<option></option>");
        for(int i = 1; i <=31; i++) {
            if(i != selectedValue)
                out.print("<option value='"+i+"'>"+i+"</option>");
            else
                out.print("<option value='"+i+"' selected='selected'>"+i+"</option>");
        }
    }
    
    public static void insertDateBoxValue(JspWriter out, String selectedValue) throws IOException {
        if(selectedValue==null || selectedValue.isEmpty())
            insertDateBoxValue(out);
        else
            insertDateBoxValue(out, Integer.parseInt(selectedValue));
    }
    
    public static void insertMonthBoxValue(JspWriter out) throws IOException{
        out.print("<option></option>");
        for(int i = 1; i <=12; i++) {
            out.print("<option value='"+i+"'>"+i+"</option>");
        }
    }
    
    public static void insertMonthBoxValue(JspWriter out, int selectedValue) throws IOException {
        out.print("<option></option>");
        for(int i = 1; i <=12; i++) {
            if(i != selectedValue)
                out.print("<option value='"+i+"'>"+i+"</option>");
            else
                out.print("<option value='"+i+"' selected='selected'>"+i+"</option>");
        }
    }
    
    public static void insertMonthBoxValue(JspWriter out, String selectedValue) throws IOException {
        if(selectedValue==null || selectedValue.isEmpty())
            insertMonthBoxValue(out);
        else
            insertMonthBoxValue(out, Integer.parseInt(selectedValue));
    }
    
    public static void insertYearBoxValue(JspWriter out) throws IOException{
        out.print("<option></option>");
        for(int i = Constants.MIN_YEAR; i <= Constants.MAX_YEAR; i++) {
            out.print("<option value='"+i+"'>"+i+"</option>");
        }
    }
    
    public static void insertYearBoxValue(JspWriter out, int selectedValue) throws IOException {
        out.print("<option></option>");
        for(int i = Constants.MIN_YEAR; i <= Constants.MAX_YEAR; i++) {
            if(i != selectedValue)
                out.print("<option value='"+i+"'>"+i+"</option>");
            else
                out.print("<option value='"+i+"' selected='selected'>"+i+"</option>");
        }
    }
    
    public static void insertYearBoxValue(JspWriter out, String selectedValue) throws IOException {
        if(selectedValue==null || selectedValue.isEmpty())
            insertYearBoxValue(out);
        else
            insertYearBoxValue(out, Integer.parseInt(selectedValue));
    }
}
